package com.guldan.jaina.manager;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.guldan.core.manager.BaseManager;
import com.guldan.core.utils.DateUtil;
import com.guldan.jaina.dto.dictionary.DictionaryDTO;
import com.guldan.jaina.dto.paysummary.PaySummaryDTO;
import com.guldan.jaina.utils.DictionaryDomainConvertUtils;
import com.guldan.jaina.utils.PaySummaryDomainConvertUtils;
import com.guldan.mapper.db.entity.Dictionary;
import com.guldan.mapper.db.entity.DictionaryExample;
import com.guldan.mapper.db.entity.PaySummary;
import com.guldan.mapper.db.entity.PaySummaryExample;
import com.guldan.mapper.db.mapper.PaySummaryMapperExt;

/**
 * Created by Silas.
 * Date: 2016/11/7
 * Time: 22:55
 */
@Service
public class PaySummaryManager extends BaseManager {

    @Autowired
    private PaySummaryMapperExt paySummaryMapperExt;

    /**
     * save pay summary
     * @param dto
     * @return
     */
    public PaySummaryDTO save(PaySummaryDTO dto) {
    	PaySummary entity;
        if (dto.getPaySummaryId() != null && dto.getPaySummaryId() > 0) {

            entity = paySummaryMapperExt.selectByPrimaryKey(dto.getPaySummaryId());
            entity.setDataDownloader(dto.getDataDownloader());
            entity.setDataDownloadTime(dto.getDataDownloadTime());
            entity.setPayCycleCode(dto.getPayCycleCode());
            entity.setSummaryId(dto.getSummaryId());
            entity.setPayFileName(dto.getPayFileName());
            entity.setPayMoney(dto.getPayMoney());
            entity.setPaySummaryId(dto.getPaySummaryId());
            entity.setPeopleCount(dto.getPeopleCount());
            entity.setRecUploader(dto.getRecUploader());
            entity.setRecUploadTime(dto.getRecUploadTime());

            entity.setDisabled(dto.getDisabled());
            entity.setModifyTime(DateUtil.getCurrentTime());
            entity.setModifier(dto.getModifier());
            paySummaryMapperExt.updateByPrimaryKeySelective(entity);
        } else {
            entity = new PaySummary();
            entity.setDataDownloader(dto.getDataDownloader());
            entity.setDataDownloadTime(dto.getDataDownloadTime());
            entity.setSummaryId(dto.getSummaryId());
            entity.setPayCycleCode(dto.getPayCycleCode());
            entity.setPayFileName(dto.getPayFileName());
            entity.setPayMoney(dto.getPayMoney());
            entity.setPaySummaryId(dto.getPaySummaryId());
            entity.setPeopleCount(dto.getPeopleCount());
            entity.setRecUploader(dto.getRecUploader());
            entity.setRecUploadTime(dto.getRecUploadTime());

            entity.setDisabled(dto.getDisabled());
            entity.setCreator(dto.getModifier());
            entity.setCreateTime(DateUtil.getCurrentTime());
            paySummaryMapperExt.insertSelective(entity);
        }
        dto = PaySummaryDomainConvertUtils.convert(entity);
        return dto;
    }

    /**
     * search pay summary
     * @param summaryId
     * @return
     */
    public List<PaySummaryDTO> all(String summaryId) {
    	PaySummaryExample example = new PaySummaryExample();
    	PaySummaryExample.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(summaryId)) {
            criteria.andSummaryIdEqualTo(summaryId);
        }
        criteria.andIsDeleteEqualTo(Boolean.FALSE);

        List<PaySummaryDTO> data = new ArrayList<PaySummaryDTO>();
        List<PaySummary> list = paySummaryMapperExt.selectByExample(example);
        if (list != null) {
            for (PaySummary entity : list) {
            	PaySummaryDTO dto = PaySummaryDomainConvertUtils.convert(entity);
                data.add(dto);
            }
        }
        return data;
    }  
}
